package com.chen.lifepic.ThumbnailFragment;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.chen.lifepic.LifePic;
import com.chen.lifepic.R;
import com.chen.lifepic.DataProvider.RemoteGrideViewProvider;
import com.chen.lifepic.thumbnailManager.IImageReadyListener;
import com.chen.lifepic.thumbnailManager.ThumbnailManager;
import com.chen.lifepic.utils.APPCommonDef.PictureType;

public class RemoteGrideViewAdapter extends BaseAdapter implements
		IImageReadyListener {
	Activity _context = null;

	// RemoteGrideViewProvider _remoGrideViewProvider = null;

	GridView _parent = null;

	public RemoteGrideViewAdapter(Activity context, GridView parent) {
		_context = context;
		// _remoGrideViewProvider = new RemoteGrideViewProvider();
		// _remoGrideViewProvider.refresh(this);
		if (null != getGrideViewProvider())
			getGrideViewProvider().refresh(this, false);
		_parent = parent;
		ThumbnailManager.getInstance().registerImageReadyListener(this);
	}

	RemoteGrideViewProvider getGrideViewProvider() {
		if (null == _context)
			return null;
		return ((LifePic) _context).getRemoteGrideViewProvider();
	}

	@Override
	public int getCount() {
		if (null == getGrideViewProvider())
			return 0;
		return getGrideViewProvider().getCount();
	}

	@Override
	public Object getItem(int position) {
		if (null == getGrideViewProvider())
			return null;
		return getGrideViewProvider().getItem(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TextView view = new TextView(_context);
		ImageView view = null;
		if (null != convertView && convertView instanceof ImageView) {
			view = (ImageView) convertView;
		} else {
			view = generateImageView();
		}
		setImageViewParameter(view);
		view.setImageResource(R.drawable.loading);
		Bitmap bmp = ThumbnailManager.getInstance().getImage(
			getGrideViewProvider().getItem(position),
			PictureType.PictureTypeThumbnail);
		if (null != bmp) {
			Log.d("image", "bmp is not null index = " + position);
			view.setImageBitmap(bmp);
		}

		return view;
	}

	ImageView generateImageView() {
		ImageView view = new ImageView(_context);
		return view;
	}

	void setImageViewParameter(ImageView view) {
		if (null == _parent)
			return;
		int parentViewWidth = _parent.getWidth() / 4;
		int parentViewHeight = parentViewWidth * 3 / 2;
		view.setLayoutParams(new AbsListView.LayoutParams(parentViewWidth,
			parentViewHeight));
		view.setScaleType(ScaleType.FIT_CENTER);
	}

	@Override
	public void imageReady(String url) {
		if (null == _parent)
			return;
		Message msg = new Message();
		msg.obj = url;
		_handler.sendMessage(msg);
	}

	final Handler _handler = new Handler() {
		@Override
		public void dispatchMessage(android.os.Message msg) {
			if (null == msg.obj)
				return;

			refreshGridViewIfNeeded((String) msg.obj);
		};
	};

	void refreshGridViewIfNeeded(String url) {
		if (null == url)
			return;

		int first = _parent.getFirstVisiblePosition();
		int last = _parent.getLastVisiblePosition();

		for (int i = first; i < last; ++i) {
			if (null == getGrideViewProvider())
				return;
			String urlInIndex = getGrideViewProvider().getThumbnailUrl(i);
			if (null == urlInIndex)
				continue;

			if (url.equals(urlInIndex)) {
				notifyDataSetChanged();
				return;
			}
		}
	}
}
